package cn.liberals.pet.controller;

import cn.liberals.basic.annotation.PreAuthorize;
import cn.liberals.basic.exception.BusinessException;
import cn.liberals.pet.service.IPetService;
import cn.liberals.pet.domain.Pet;
import cn.liberals.pet.query.PetQuery;
import cn.liberals.basic.utils.AjaxResult;
import cn.liberals.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    public IPetService petService;


    /**
     * 保存和修改公用的
     * @param pet  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "宠物类型新增或修改", sn = "pet:put")
    public AjaxResult addOrUpdate(@RequestBody Pet pet){
        try {
            if( pet.getId()!=null)
                petService.update(pet);
            else
                petService.save(pet);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "宠物类型删除数据", sn = "pet:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            petService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    @PreAuthorize(name = "宠物类型根据Id查询", sn = "pet:get")
    public Pet get(@PathVariable("id")Long id)
    {
        return petService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Pet> list(){

        return petService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @PreAuthorize(name = "宠物类型分页查询", sn = "pet:list")
    public PageList<Pet> json(@RequestBody PetQuery query)
    {
        return petService.queryData(query);
    }

    /* 
    * @Title: onsale
    * @Description: 批量上架宠物
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/17 0:04  
    * @Parameters: 
    * @Return 
    */
    @PostMapping("/onsale")
    @PreAuthorize(name = "宠物类型批量上架宠物", sn = "pet:onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids){
        try {
            petService.onsale(ids);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /* 
    * @Title: offsale
    * @Description: 宠物批量下架
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/17 0:12  
    * @Parameters: 
    * @Return 
    */
    @PostMapping("/offsale")
    @PreAuthorize(name = "宠物类型宠物批量下架", sn = "pet:offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        try {
            petService.offsale(ids);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }
}
